import processing.core.PApplet;
import processing.core.PImage;

public class CopyOfAssasinPoul extends PApplet {

	PImage fondo;

	float inc = 0.0f;
	float posx = 250;
	float posy = 250;
	float angle3 = 0.0f;
	boolean food = false;
	int foodiam = 0;
	int fadestroke;
	float anglefood;

	// velocidad
	float easing = 1.0f;
	float targetY;
	float targetX;

	TexturedSphere texturedSphere = new TexturedSphere();
	Sphere sphere = new Sphere();
	
	Pulpo pulpo = new Pulpo();

	public void setup() {
		targetX = random(width);
		targetY = random(height);
		size(1024, 768);
		hint(ENABLE_OPENGL_4X_SMOOTH);
		stroke(0, 204);
		smooth();
		frameRate(30);
		fondo = loadImage("fondo.png");
		rectMode(CENTER);

		//texturedSphere.setup(this);
		sphere.setup(this);
	}

	public void draw() {

		if (mouseX > 100 && mouseX < width - 100) {
			targetX = mouseX;
			targetY = mouseY;
		}

		// image (fondo,0,0);
		// fill(255,90);
		// rect(250,250,width,height);
		background(0, 1);
		
		//texturedSphere.draw(this, mouseX, mouseY);
		//sphere.draw(this);
		
		fill(111, 54, 185);
		// rect(0,0,width,height);

		if (food == true) {
			createFood(targetX, targetY);
			// createFood(targetX-5, targetY-5);
			// createFood(targetX-5, targetY+5);
		}
		
		pulpo.posx = mouseX;
		pulpo.posy = mouseY;
		
		pulpo.draw(this);

		// if (dist(targetX, targetY, posx, posy) < 20) {
		// targetX = random(width);
		// targetY = random(height);
		// foodiam = 10;
		// fadestroke = 255;
		// food = true;
		// }

//		inc += 0.006f;
//		float angle1 = sin(inc) / 10.0f + sin(inc * 4.5f) / 70.0f;
//		float angle2 = cos(inc) / 12.0f + cos(inc * 4.5f) / 80.0f;

		//angle3 += 0.007f;

		// posx=250+cos(angle3)*100+sin(angle3)*100;
		// posy=250+tan(cos(angle3))*100+cos(angle3)*25;

//		posx += (targetX - posx) * easing;
//		posy += (targetY - posy) * easing;
//		stroke(0, 204);
//		translate(posx, posy);
//		tail(0, 24, angle1 * 4);
//		rotate(sin(rot1) + QUARTER_PI);
//		tail(0, 21, angle1 * 2.7f);
//		rotate(cos(rot2) + QUARTER_PI);
//		tail(0, 18, angle2 * 5.5f);
//		rotate(rot3 + QUARTER_PI);
//		tail(0, 17, angle1 * 3);
//		rotate(sin(rot4 + QUARTER_PI));
//		tail(0, 19, angle2 * 4.2f);
//		rotate(atan(rot5) + QUARTER_PI);
//		tail(0, 20, angle1 * 2.4f);
//		rotate(sin(rot6) + QUARTER_PI);
//		tail(0, 24, angle2 * 3);
//		rotate(cos(rot7) + QUARTER_PI);
//		tail(0, 25, angle1 * 3.2f);

//		rot1 += a1;
//		rot2 += a2;
//		rot3 += a3;
//		rot4 += a4;
//		rot5 += a5;
//		rot6 += a6;
//		rot7 += a7;
//		rot8 += a8;
	}

	public void tail(int x, int units, float angle) {
		pushMatrix();
		translate(x, 0);
		
		float size = 1;
		
		units = (int)(units * (1/size));
		
		for (int i = units; i > 0; i--) {

			strokeWeight(sqrt(i / 6));
			line(-i, 0, i, 0);
			fill(255);
			ellipse(-i, 0, 2, 2);
			ellipse(i, 0, 2, 2);
			strokeWeight(i / 1.7f);
			stroke(255,60);
			fill(111, 54, 185);
			ellipse(0, 0, sqrt(i), sqrt(i));
			line(0, 0, 0, -8);
			translate(0, -10);
			rotate(angle);
		}
		popMatrix();
	}
	
	
//	public void tail(int x, int units, float angle) {
//
//		pushMatrix();
//		translate(x, 0);
//
//		for (int i = units; i > 0; i--) {
//
//			strokeWeight(sqrt(i / 6));
//			line(-i, 0, i, 0);
//			fill(255);
//			ellipse(-i, 0, 2, 2);
//			ellipse(i, 0, 2, 2);
//			stroke(255, 76);
//			strokeWeight(i / 1.7f);
//			fill(111, 54, 185);
//			ellipse(0, 0, sqrt(i), sqrt(i));
//			line(0, 0, 0, -8);
//			translate(0, -10);
//			rotate(angle);
//		}
//		popMatrix();
//	}

	public void mousePressed() {
		// saveFrame("pulpo-###.png");
		targetX = mouseX;
		targetY = mouseY;
		foodiam = 10;
		fadestroke = 255;
		food = true;
	}

	public void createFood(float locationX, float locationY) {
		fill(0);
		ellipse(locationX, locationY, 3 + abs(cos(anglefood) * 3),
				3 + abs(cos(anglefood) * 3));
		fill(0, 10);
		ellipse(locationX, locationY, abs(cos(anglefood) * 2),
				abs(cos(anglefood) * 2));
		noFill();
		stroke(0, 80);
		ellipse(locationX, locationY,
				10 + abs(cos(anglefood + QUARTER_PI) * 3),
				10 + abs(cos(anglefood + QUARTER_PI) * 3));
		if (foodiam < 220) {
			noFill();
			stroke(0, fadestroke);
			ellipse(locationX, locationY, foodiam, foodiam);
			foodiam++;
			fadestroke -= 2;
		}
		anglefood += map(dist(targetX, targetY, posx, posy), 0, 300, 0.2f,
				0.01f);
	}

	static public void main(String args[]) {
		PApplet.main(new String[] { "--bgcolor=#DFDFDF", "AssasinPoul" });
	}
}
